package com.itheima.mobile;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfo;
import com.itheima.service.OrderService;
import com.itheima.utils.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/11-17:49
 * @Version: 1.0.0
 */
@RequestMapping("/order")
@RestController
public class OrderController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisUtils jedisUtils;

    @RequestMapping("/submit")
    public Result submit(@RequestBody OrderInfo orderInfo){
//        获取令牌
        String token = orderInfo.getToken();
//        redis是单线程, 每个用户请求都需要排队
        if(token==null){
            return Result.ERROR("非法请求");
        }
        if(jedisUtils.del(token)==0){
            return Result.ERROR("不要重复请求");
        }

//
//          从redis获取验证码
            String validateCode = jedisUtils.get(orderInfo.getTelephone()+RedisMessageConstant.SENDTYPE_ORDER);
//            验证验证码
            if(validateCode==null || !validateCode.equals(orderInfo.getValidateCode())){
                jedisUtils.setex(token,60*15,token);
                return Result.ERROR("验证码错误");
            }
            orderInfo.setOrderType(Order.ORDERTYPE_WEIXIN);
//            调用服务方法
        Result result=null;
//        帮用户尝试几次

        try {
           result= orderService.order(orderInfo);
           return result;
        }catch (OptimisticLockingFailureException e){
            int max = 5;
            while (max>0){
                try {
                    result=orderService.order(orderInfo);
                    return result;
                }catch (OptimisticLockingFailureException e1){
                    e1.printStackTrace();
                    max--;

                    if(max==0){
                        System.out.println("重试了"+max+"次,还不成功"+ JSON.toJSONString(orderInfo));
                    }
                }

            }
            return Result.ERROR(MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findById(id);
            return Result.OK(MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR(MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/getToken")
    public Result getToken(){
//        获取随机数
        String token = UUID.randomUUID().toString();
//        加入redis中
        jedisUtils.setex(token,60*15,token);
        return Result.OK("",token);

    }
}
